package com.cx.swagger.controller;

import com.cx.swagger.pojo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @Author ThinkPad
 * @CreationDate 2021/9/7 10:07
 * @Description 。。。
 */
@Api(tags = "用户模块API")
@RestController
@RequestMapping("/user")
public class UserController {

    static Map<Long, User> userMap
            = Collections.synchronizedMap(new HashMap<>());

    @GetMapping("/")
    @ApiOperation("获取用户列表")
    public List<User> getUser(){
        return new ArrayList<>(userMap.values());
    }

    @GetMapping("/{userId}")
    @ApiOperation(value = "获取用户详细信息", notes = "根据url的id来获取用户详细信息")
    public User getUser(@PathVariable Long userId){
        return userMap.get(userId);
    }

    @PostMapping("/")
    @ApiOperation("新增用户信息")
    public String postUser(@RequestBody User user){
        userMap.put(user.getUserId(),user);
        return "success";
    }

    @PutMapping("/{userId}")
    @ApiOperation("修改用户信息")
    @ApiImplicitParam(paramType = "path", dataType = "Long", name = "userId", value = "用户编号", required = true, example = "1")
    public String putUser(@PathVariable Long userId,@RequestBody User user){
        //根据ID获取原有用户的信息
        User u = userMap.get(userId);
        //更新用户数据
        u.setUserName(user.getUserName());
        u.setAge(user.getAge());
        //数据存入map
        userMap.put(userId,u);
        return "success";
    }

    @DeleteMapping("/{userId}")
    @ApiOperation(value = "删除用户", notes = "根据url的id来指定删除对象")
    public String deleterUser(@PathVariable Long userId){
        userMap.remove(userId);
        return "success";
    }
}
